﻿using CCSSolderUI.Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;

namespace CCSSolderUI.ViewModels.Dialogs.JobConfig
{
    public class AddPointDialogViewModel : BindableBase, IDialogAware
    {
        #region 变量

        #endregion
        #region 属性
        private string name;
        public string Name
        {
            get { return name; }
            set { SetProperty(ref name, value); }
        }
        private double x;
        public double X
        {
            get { return x; }
            set { SetProperty(ref x, value); }
        }
        private double y;
        public double Y
        {
            get { return y; }
            set { SetProperty(ref y, value); }
        }
        private double z;
        public double Z
        {
            get { return z; }
            set { SetProperty(ref z, value); }
        }
        private string remark;
        public string Remark
        {
            get { return remark; }
            set { SetProperty(ref remark, value); }
        }
        #endregion
        #region 方法绑定
        private DelegateCommand<object> operateCommad;
        public DelegateCommand<object> OperateCommad =>
            operateCommad ?? (operateCommad = new DelegateCommand<object>(ExecuteOperateCommad));
        private DelegateCommand getPosCommand;
        public DelegateCommand GetPosCommand =>
            getPosCommand ?? (getPosCommand = new DelegateCommand(ExecuteGetPosCommand));

        void ExecuteGetPosCommand()
        {
            if (GTSCard.Instance.OpenCardOk)
            {
                X = Math.Round(GTSCard.Instance.GetEnc(GTSCard.Instance.X1), 3);
                Y = Math.Round(GTSCard.Instance.GetEnc(GTSCard.Instance.Y1), 3);
                Z = Math.Round(GTSCard.Instance.GetEnc(GTSCard.Instance.Z1), 3);
            }
        }
        void ExecuteOperateCommad(object obj)
        {
            switch (obj.ToString())
            {
                case "确认":
                    DialogParameters param = new DialogParameters();
                    param.Add("Name", Name);
                    param.Add("X", X);
                    param.Add("Y", Y);
                    param.Add("Z", Z);
                    param.Add("Remark", Remark);
                    RequestClose?.Invoke(new DialogResult(ButtonResult.Yes, param));
                    break;
                case "取消":
                    RequestClose?.Invoke(new DialogResult(ButtonResult.None));
                    break;
                default:
                    break;
            }
        }
        #endregion
        #region 导航
        public string Title => "添加点位";

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            Name = parameters.GetValue<string>("Name");
            X = 0;
            Y = 0;
            Z = 0;
        }
        #endregion
    }
}
